﻿//------------------------------------------------------------------------------ 
// <copyright file="JComplete.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JAlert.alert.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JAlert.important.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JAlert.info.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JAlert.title.gif", "image/gif")]

namespace FmjQuery
{
    using System;
    using System.ComponentModel;
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// jQuery TextBox
    /// </summary>
    [ToolboxData("<{0}:JComplete runat=server MinChars=3 />")]
    [DefaultProperty("TargetControlID")]
    [NonVisualControl]
    public class JComplete : JQueryExtendControl
    {
        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JComplete class.
        /// </summary>
        public JComplete()
        {
            this.IsRenderControl = false;
        }
        #endregion

        #region Property AutoComplete
        /// <summary>
        /// Gets or sets address of question
        /// </summary>
        /// <example>/Handler/test.ashx</example>
        [Description("Url of page that request data")]
        public string UrlQuestion
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets RootNode for local Site
        /// </summary>
        [Description("If use local page, can insert root Node of WebSite Solution")]
        public string RootNode
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets name of client function for format record
        /// </summary>
        /// <example>function pippo(row){}</example>
        [Description("Name of client function that call when control need format record")]
        public string FormatClientFunction
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when select item
        /// </summary>
        /// <example>function pippo(item){item.extra}</example>
        [Description("Name of client function that call when client select item")]
        public string SelectClientFunction
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets extra params
        /// </summary>
        /// <example>IDRecord: 12, Name: 'Luca'</example>
        [Description("Extra params for request page")]
        public string ExtraParams
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets minimun of char for search (Default = 3)
        /// </summary>
        [DefaultValue("3")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("Min number of char for start search")]
        public int MinChars
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets inpunt css class
        /// </summary>
        [Description("Css class for input TextBox")]
        public string InputCssClass
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets result css class
        /// </summary>
        [Description("Css class for result list")]
        public string ResultsCssClass
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets Loading css class
        /// </summary>
        [Description("Css class for input TextBox when wait result")]
        public string LoadingCssClass
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets Line Separetor chars (Default = \n)
        /// </summary>
        [Description("Chars for split row")]
        public string LineSeparator
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets Cell Separetor chars (Default = |)
        /// </summary>
        [Description("Chars for split column")]
        public string CellSeparator
        {
            get;
            set;
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Gets resources
        /// </summary>
        /// <returns>collection of resource</returns>
        public override JWebSources GetResources()
        {
            if (string.IsNullOrEmpty(this.InputCssClass) || string.IsNullOrEmpty(this.LoadingCssClass) || string.IsNullOrEmpty(this.ResultsCssClass))
            {
                return new JWebSources { new JSource(Properties.Settings.Default.Css_AutoComplete_Name, Properties.Settings.Default.Css_AutoComplete_Url, JSourceType.css_dynamic, true) };
            }

            return null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return new CssResources { new CssImage(Properties.Settings.Default.Css_AutoComplete_Name, "IMG_LOADING", Properties.Settings.Default.Image_Complete_Loading, true) };
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            Manager.AutoComplete = true;

            if (this.Enabled)
            {
                // check params
                if (string.IsNullOrEmpty(this.UrlQuestion))
                {
                    throw new Exception("You must insert url question");
                }

                // create and insert script into page
                StringBuilder builder = new StringBuilder("$('document').ready(function() {");

                builder.AppendFormat("$control = $('#{0}');", this.TargetControlClientID);
                builder.AppendFormat("$control.autocomplete('{0}',", string.Concat(this.RootNode, new Control().ResolveUrl(this.UrlQuestion)));
                builder.Append("{");
                builder.AppendFormat("minChars: {0},", this.MinChars);
                builder.Append("matchSubset: 1,");
                builder.Append("matchContains: 1,");
                builder.Append("cacheLength: 10,");

                if (!string.IsNullOrEmpty(this.InputCssClass))
                {
                    builder.AppendFormat("inputclass: '{0}',", this.InputCssClass);
                }

                if (!string.IsNullOrEmpty(this.ResultsCssClass))
                {
                    builder.AppendFormat("resultsClass: '{0}',", this.ResultsCssClass);
                }

                if (!string.IsNullOrEmpty(this.LoadingCssClass))
                {
                    builder.AppendFormat("loadingClass: '{0}',", this.LoadingCssClass);
                }

                if (!string.IsNullOrEmpty(this.SelectClientFunction))
                {
                    builder.AppendFormat("onItemSelect: {0},", this.SelectClientFunction);
                }

                if (!string.IsNullOrEmpty(this.FormatClientFunction))
                {
                    builder.AppendFormat("formatItem: {0},", this.FormatClientFunction);
                }

                if (!string.IsNullOrEmpty(this.LineSeparator))
                {
                    builder.AppendFormat("lineSeparator: '{0}',", this.LineSeparator);
                }

                if (!string.IsNullOrEmpty(this.CellSeparator))
                {
                    builder.AppendFormat("cellSeparator: '{0}',", this.CellSeparator);
                }

                if (!string.IsNullOrEmpty(this.ExtraParams))
                {
                    builder.Append("extraParams: {");
                    builder.Append(this.ExtraParams);
                    builder.Append("},");
                }

                builder.Append("selectOnly: 1");

                builder.Append("},");
                builder.Append("$control.val());});");

                Utility.RegisterStartUpScript(Page, string.Concat("auto_", this.ClientID), builder.ToString());
            }
        }
        #endregion
    }
}
